package com.jxinternet.platform.services.util;

import com.jxinternet.platform.services.protocol.Constants;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Created by gold-jx on 2017/6/28.
 */
public class SecurityUtil {

    /**
     * 认证
     *
     * @param userName 约定用来计算token的参数
     * @Description : 身份验证token值算法： 算法是：将特定的某几个参数一map的数据结构传入， 进行字典序排序以后进行md5加密,32位小写加密；
     * @Method_Name : authentication
     */
    public static String authentication(String userName) {

        try {
            Map srcData = new HashMap();
            srcData.put("userName", userName);
            srcData.put("platformNo", Constants.PLATFORM_NO);

            List<Entry<String, Object>> list = new ArrayList<Entry<String, Object>>(srcData.entrySet());
            Collections.sort(list, new Comparator<Entry<String, Object>>() {
                //升序排序
                public int compare(Entry<String, Object> o1, Entry<String, Object> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });

            StringBuffer srcSb = new StringBuffer();
            for (Entry<String, Object> srcAtom : list) {
                srcSb.append(String.valueOf(srcAtom.getValue()));
            }
            //计算token
            String token = MD5Util.getMD5to16(srcSb.toString());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
